package cn.vendpay.com.config;

import cn.vendcommon.com.entity.constants.MQConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class RabbitConfig {
    public static final String EXCHANGE_NAME = MQConstant.INVENTORY_EXCHANGE;
    public static final String QUEUE_NAME = MQConstant.INVENTORY_QUEUE;
    public static final String ROUTING_KEY = MQConstant.INVENTORY_ROUTING_KEY;

    // 死信队列与交换机
    public static final String INVENTORY_DLX = MQConstant.INVENTORY_DLX;
    public static final String INVENTORY_DLQ = MQConstant.INVENTORY_DLQ;

    public static final String INVENTORY_ROLLBACK_EXCHANGE = MQConstant.ROLLBACK_EXCHANGE;
    public static final String INVENTORY_ROLLBACK_QUEUE = MQConstant.ROLLBACK_QUEUE;
    public static final String INVENTORY_ROLLBACK_ROUTING_KEY = MQConstant.ROLLBACK_ROUTING_KEY;

    /**
     * 正常队列（带死信参数）
     * @return
     */
    @Bean
    public Queue queue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", INVENTORY_DLX);        // 指定死信交换机
        args.put("x-dead-letter-routing-key", INVENTORY_DLQ);     // 指定死信队列路由
        return new Queue(QUEUE_NAME, true, false, false, args);
    }

    // 死信队列
    @Bean
    public Queue deadLetterQueue() {
        return new Queue(INVENTORY_DLQ, true);
    }

    // 回滚队列
    @Bean
    public Queue rollbackQueue() {
        return new Queue(INVENTORY_ROLLBACK_QUEUE, true);
    }

    // 正常交换机
    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(EXCHANGE_NAME, true, false);
    }

    // 死信交换机
    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(INVENTORY_DLX, true, false);
    }

    // 回滚交换机
    @Bean
    public DirectExchange rollbackExchange() {
        return new DirectExchange(INVENTORY_ROLLBACK_EXCHANGE, true, false);
    }

    // 正常队列绑定交换机
    @Bean
    public Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }

    // 死信队列绑定死信交换机
    @Bean
    public Binding dlqBinding(Queue deadLetterQueue, DirectExchange deadLetterExchange) {
        return BindingBuilder.bind(deadLetterQueue).to(deadLetterExchange).with(INVENTORY_DLQ);
    }

    // 回滚队列绑定回滚交换机
    @Bean
    public Binding rollbackBinding() {
        return BindingBuilder.bind(rollbackQueue())
                .to(rollbackExchange())
                .with(INVENTORY_ROLLBACK_ROUTING_KEY);
    }




    // JSON 消息转换器
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    // RabbitTemplate 使用 JSON 转换器
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter converter) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(converter);
        return template;
    }

    // 自定义监听容器工厂，启用手动 ack
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            ConnectionFactory connectionFactory,
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            MessageConverter converter) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        factory.setMessageConverter(converter);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        factory.setConcurrentConsumers(2);
        factory.setMaxConcurrentConsumers(10);
        return factory;
    }

}
